package com.spring.service.impl;

import com.spring.dao.AccountDao;
import com.spring.dao.AccountDaoImpl;
import com.spring.entity.Area;
import com.spring.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("accountService")
public class AccountServiceImpl implements AccountService {




    @Autowired
    private AccountDao accountDao;

    public List<Area> queryArea() {
        return accountDao.queryArea();
    }


    public Area queryAreaById(int areaId) {
        int i = 1/0;
        return accountDao.queryAreaById(areaId);
    }



    public boolean insertArea(Area area) {

        if(area != null && !"".equals(area.getAreaName())){
            area.setCreateTime(new Date());
            area.setLastEditTime(new Date());
            try{
                int i = accountDao.insertArea(area);
                if(i > 0){
                    return true;
                }else {
                    throw  new RuntimeException("插入区域信息错误");
                }
            }catch (Exception e){
                throw  new RuntimeException("插入区域信息错误" + e.getMessage());
            }

        }
        return false;
    }




    public boolean updateArea(Area area) {
        if(area.getAreaId() != null & area.getAreaId()>0){
            area.setLastEditTime(new Date());
            try{
                int i = accountDao.updateArea(area);
                if(i > 0){
                    return true;
                }else {
                    throw  new RuntimeException("更新区域信息错误");
                }
            }catch (Exception e){
                throw  new RuntimeException("更新区域信息错误" + e.getMessage());
            }
        }
        return false;
    }


    public boolean deleteArea(int areaId) {


        if(areaId>0){
            try{
                int i = accountDao.deleteArea(areaId);
                if(i > 0){
                    return true;
                }else {
                    throw  new RuntimeException("删除区域信息错误");
                }
            }catch (Exception e){
                throw  new RuntimeException("删除区域信息错误" + e.getMessage());
            }
        }
        return false;
    }
}
